package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-mgrs/cache"
	"novel-mgrs/models"
	"novel-mgrs/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	var user models.User
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不为空",
		})
		return
	}
	redis_key := "user_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	err := models.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户无法查询",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次,封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
	})

}

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	var life = models.Life{
		Title:   title,
		Content: content,
	}
	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
	}
	err := models.DB.Create(&life).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	redis_key := "life_id_" + strconv.Itoa(int(life.ID))
	var data models.Life
	jsonData, _ := json.Marshal(data)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})

}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var count int64
	var life []models.Life
	models.DB.Model(&models.Life{}).Count(&count)
	redis_key := "life_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Offset(offset).Limit(size).Find(&life)
		jsonData, _ := json.Marshal(life)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &life)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"data":  life,
			"count": count,
		},
	})
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var life models.Life
	err := models.DB.Where("id = ?", id).First(&life).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	redis_key := "detail" + strconv.Itoa(int(life.ID))
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Where("id=?", id).First(&life)
		jsonData, _ := json.Marshal(life)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &life)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"life": life,
	})
}

func Del(c *gin.Context) {
	id := c.Query("id")
	var life models.Life
	err := models.DB.Where("id = ?", id).Delete(&life).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "删除失败",
		})
		return
	}
	redis_key := "life_id_" + id
	cache.Rdb.Del(cache.Ctx, redis_key)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}
